package com.forest.gissingle.sql;

import java.io.File;
import java.util.ArrayList;

import com.forest.gissingle.GisApplicationis;
import com.forest.gissingle.bean.SqlDataBean;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.util.SparseArray;

public class DBManager {
	private static String TAG = "sptest";
	// private static final String DB_NAME = "battery.db";
	private static final int version_total = 1;
	// SQL
	public static final String DB_TOTAL = "SQLTotal.db";

	// tables
	public static final String AREA_TABLE = "area";
	public static final String MOBILE_TABLE = "mobile";
	public static final String WIFI_TABLE = "wifi";
	public static final String UID_TABLE_FLAG = "uid";

	SQLiteDatabase database;
	private static DBHelperTotal dbHelperTotal;
	private static DBManager dbManager;

	/**
	 * 保存uid与uid对应报名的静态变量
	 */
	public static SparseArray<String> uidPacs;

	// 初始化
	DBManager(Context context) {
		dbHelperTotal = new DBHelperTotal(context, DB_TOTAL, null,
				version_total);
	}

	/**
	 * 获取实例
	 * 
	 * @return
	 */
	public static synchronized DBManager getInstance() {
		if (dbManager == null || dbHelperTotal == null) {
			dbManager = new DBManager(GisApplicationis.getInstance());
			// 初始化数据库
			File file = new File(DBUtils.getDBPath(GisApplicationis
					.getInstance()));
			if (!file.exists()) {
				DBUtils.copyDBFromAsset(GisApplicationis.getInstance());
			}
			Log.d(TAG, "DBManagergetInstance");
		}
		return dbManager;
	}

	// 实际使用不需要插入数据
	// public void insertData(SqlDataBean sqlBean) {
	// dbHelperTotal.insertSqlData(sqlBean);
	// }
	//
	// public void insertSqlDataArray(ArrayList<SqlDataBean> sqlBeanArray) {
	// dbHelperTotal.insertSqlDataArray(sqlBeanArray);
	// }

	/**
	 * 依据region与number提取数据
	 * 
	 * @param region
	 * @param number
	 * @return
	 */
	public SqlDataBean searchBean(int region) {
		return dbHelperTotal.searchDB(region);
	}

	/**
	 * 将封装好的插入、更新或删除的sql语句传入 样例： DBManager.getInstance().execSqls( new String[]
	 * {"insert into mode(name,descript,screen_light) values('a','b','1)"} );
	 * 更新：
	 * "update mode set name='bb',descript='vvvv',screen_light='233' where id=1"
	 * 
	 * @param sqls
	 */
	public void execSqls(Object[] sqls) {
		database = dbHelperTotal.getWritableDatabase();
		database.close();
		if (database.isOpen()) {
			database.close();
		}
		// try {
		// database.beginTransaction();
		// for (Object sql : sqls) {
		// database.execSQL(sql.toString());
		// }
		//
		// database.setTransactionSuccessful();
		// } catch (Exception e) {
		// e.printStackTrace();
		// // database.endTransaction();
		// } finally {
		// database.endTransaction();
		// database.close();
		// }
	}

	/**
	 * 将封装好的sql语句传入
	 * 
	 * @param sqls
	 */
	public void execSqls(ArrayList<String> sqls) {
		Object[] strings = sqls.toArray();
		execSqls(strings);
	}

	/**
	 * 获取数据
	 * 
	 * @param sql
	 * @return 查询游标Cursor,Cursor为空，则搜索失败(搜索条件错误)。
	 *         判断搜索结果为空，要用cur.moveToFirst()=false；
	 *         查询完毕要执行closeDatabase(cursor),关闭游标与数据库 sql样例："SELECT * FROM mode"
	 *         if (cur != null) { try { int minup =
	 *         cur.getColumnIndex("descript"); int mindown =
	 *         cur.getColumnIndex("screen_light"); if (cur.moveToFirst()) {
	 *         String oldup0 = cur.getString(minup); String olddown0 =
	 *         cur.getString(mindown); L.d(TAG, oldup0+olddown0); } } catch
	 *         (Exception e) { L.d(TAG, "数据库搜索失败"); } }
	 */
	public Cursor query(String sql) {
		return query(sql, null);
	}

	public Cursor query(String sql, String[] args) {
		Cursor cursor = null;
		try {
			database = dbHelperTotal.getReadableDatabase();
			cursor = database.rawQuery(sql, args);

			return cursor;
		} catch (Exception e) {
			e.printStackTrace();
			// database.close();
			// } finally {
			// database.close();
		}

		return null;
	}

	/**
	 * 执行完查询操作后，需要关闭游标与数据库
	 * 
	 * @param cur
	 */
	public void closeDatabase(Cursor cur) {
		if (cur != null) {
			cur.close();
		}
		if (database != null & database.isOpen()) {
			database.close();
		}
	}

}
